import Vue, { AsyncComponent } from 'vue'
import Router, { RouteConfig, Route, NavigationGuard } from 'vue-router'
import storage from 'assets/js/storage/index.js'


const Main: AsyncComponent = (): any => import('@/Main.vue')
const Login: AsyncComponent = (): any => import('@/Login.vue')

const Index: AsyncComponent = (): any => import('@/components/Index.vue')
const Student: AsyncComponent = (): any => import('@/components/student/Student.vue')
const Imports: AsyncComponent = (): any => import('@/components/student/Imports.vue')
const Book: AsyncComponent = (): any => import('@/components/book/Book.vue')
const Level: AsyncComponent = (): any => import('@/components/level/Level.vue')
const Word: AsyncComponent = (): any => import('@/components/word/Word.vue')


Vue.use(Router)


// async loadConfig(cb:any) {

//   let { data } = await findDataByCategory({})
//   if (data.success) {
//     console.log("ddd")
//     this.$root.$data.config = data.data
//   cb&&cb()
//   }
// }

const routes: RouteConfig[] = [

  {
    path: '/',
    component: Index
  }, {
    path: '/index',
    component: Index
  },
  {
    path: '/main',
    component: Main,
    children: [{
      path: '/student',
      component: Student,
      children: []
    }, {
      path: '/imports',
      component: Imports
    }, {
      path: '/book',
      component: Book
    }, {
      path: '/level',
      component: Level,
      meta:{keepAlive:true}
    }, {
      path: '/word',
      component: Word
    }
    ]
  }, {
    path: '/login',
    component: Login
  }
]

const router: Router = new Router({
  // mode: 'history',
  base: '/',
  routes
})
// const beforeEach=function(to:any,from:any,next:any){
//   const sess=sessionStorage
//   const toRoute=to.path
//   const fromRoute=to.path
//   let h=JSON.parse(sess.getItem(toRoute) as any)
//   console.log('hh',h)
//   if(h&&h.history){
//     h.history=false
//     sess.removeItem(toRoute)
//   }else{
//     sess.setItem(fromRoute||'/',JSON.stringify({history:true}))
//   }
//   next()
// }
router.beforeEach(async(to, from, next) => {
  if(!storage.getAccessToken()&&to.path!=='/login'){
    next('/login')
  }
  // beforeEach(to,from,next)
  next()
})

export default router
